package com.ykdl.member.kid.gears;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.DoctorTalkChildListAdapter;
import com.ykdl.member.kid.adapters.FreakyChatChildListAdapter;
import com.ykdl.member.kid.beans.DoctorTalkChildListBean;
import com.ykdl.member.kid.beans.FreakyChatChildListBean;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.gears.DoctorTalkChildListActivity.DoctorTalkChildListTag;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.util.RequestAsAccessTokenUtil;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class FreakyChatChildListActivity extends AbsFragmentActivity {
	
	private FreakyChatChildListBean freakyChatChildListBean;
	private FreakyChatChildListAdapter adapter;
	private int gridview_item_image_height;
	private HttpBaseRequest request;
	
	
	@Override
	protected void getRequestData() {
		showProgress(this);
		final String  url = KidConfig.BASE_TEST_SERVER + "catalogs";
		HttpParameters   urlParams = new HttpParameters();
		urlParams.put("cursor", cursor + "");
		urlParams.put("count", count + "");
		urlParams.put("tag", "groups");
		
		request = Wxxr.getInstance().getRequest(url, urlParams);
		TaskManager.startHttpRequest(request, new FreakyChatChildListTag(), FreakyChatChildListBean.class);
	}
	
	class FreakyChatChildListTag implements ITag{

		@Override
		public void refreshUI(Object t, DataState ds) {
			freakyChatChildListBean = (FreakyChatChildListBean)t;
			if(freakyChatChildListBean.isExpired()){
				RequestAsAccessTokenUtil.getInstance(mContext).refreshTokenRequest(request, this, FreakyChatChildListBean.class);
			}else{
				showdata();
				dismissProgress();
			}
		}
		@Override
		public boolean refreshError(DataParseError parseError) {
			//下拉加载更多时，请求数据错误时，取消正在加载状态
			removeFootRefreshView();
			dismissProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

		final CatalogBean  catalog = this.freakyChatChildListBean.getCatalogs().get(position);
		Intent  intent = null;
		TopicBean  topic = null;
		if(catalog.getCatalog_type() == 1){
			if(catalog.getItems().size() > 1){ //多个话题(进入专题列表页)
				intent =new Intent(this,TopicsSheetListActivity.class);
				intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
			}else{//单个话题(进入话题详情页)
				if(catalog.getCatalogItems() == null || catalog.getCatalogItems().size() == 0 || "[]".equals(catalog.getCatalogItems()))return;
				topic = ((CatalogTopicItem)catalog.getCatalogItems().get(0)).getItem();
				if(topic == null)return;
				intent =new Intent(this,TopicAndResultActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getTopic_id());
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getSource_id());
			}
			this.startActivity(intent);
		}
	}

	@Override
	protected void setTitle() {
		this.tv_title.setText("");
		this.tv_title.setText("辣妈聊育儿");

	}

	@Override
	protected void matchGridView() {
		final int screenWidth = MobileUtils.getScreenWidthIntPx();
		final int gridview_width = (screenWidth - MobileUtils.dpToPx(12));
		gridview_item_width = gridview_width / 2; 
		gridview_item_image_height = (int)((gridview_item_width- MobileUtils.dpToPx(12)) / 2.8);
		
		LayoutParams  params = (LayoutParams) mGridView.getLayoutParams();
		params.width = gridview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		mGridView.setLayoutParams(params);
		
		
		mGridView.setHorizontalSpacing(MobileUtils.dpToPx(6));
		mGridView.setVerticalSpacing(MobileUtils.dpToPx(6));
		mGridView.setColumnWidth(gridview_item_width);
		mGridView.setCacheColorHint(getResources().getColor(R.color.touming));
		mGridView.setNumColumns(2);
	}

	@Override
	protected void showdata() {
		
		//加载更多数据完成
		removeFootRefreshView();
		
		if(freakyChatChildListBean.getNext_cursor() == freakyChatChildListBean.getTotal_number())
		{
			mPullToRefreshView.setEnablePullLoadMoreDataStatus(false);
		}
		
		if(this.freakyChatChildListBean.getCatalogs() != null && this.freakyChatChildListBean.getCatalogs().size() > 0){
			if(adapter == null){
				adapter = new FreakyChatChildListAdapter(this.mContext, bitmapUtil, gridview_item_image_height);
			}
			adapter.setList(this.freakyChatChildListBean.getCatalogs());
			mGridView.setAdapter(adapter);
			this.cursor = freakyChatChildListBean.getNext_cursor();
			this.total_count = freakyChatChildListBean.getTotal_number();
			adapter.notifyDataSetChanged();
		}else{
			if(isFootRefresh){
				Toast.makeText(this, "没有更多数据！", 0).show();
			}else{
				Toast.makeText(this, "没有数据哦！", 0).show();
			}
		}
		
		
	}
	

}
